import os
import subprocess
import configparser

conf = configparser.ConfigParser()
inipath = r"Common/config/Debug/Debug.ini"

def getproject():
    path = r"C:\\Users\\huixin.ma\\Desktop\\Check"
    return os.listdir(path)
def operatepath(path=None):
    if path is None:
        conf.read(inipath, encoding='utf-8')
        return conf.get('Check', 'dppath')
    else:
        conf.set('Check', 'dppath', path)
        conf.write(open(inipath, 'w', encoding='utf-8'))


def generate_exif(dppath, jpgpath):
    exifpath = os.path.join(os.getcwd(), 'exif')
    if not os.path.exists(exifpath):  # 如果路径不存在
        os.mkdir(exifpath)

    # 生成以输入图片名（包括后缀）命名的exif文件
    child = subprocess.Popen(
        f"{dppath} -dump {jpgpath} -save {exifpath}"
    )
    child.wait()

    exif_file_path = os.path.join(exifpath, os.path.basename(jpgpath) + '.exif')
    return exif_file_path


def extract_exif_info(exif_file_path):
    # 从 exif 文件路径中提取图片名称（假设图片名称在生成的 exif 文件名中）
    image_name = os.path.basename(exif_file_path).replace('.exif', '')

    # 读取生成的 exif 文件内容
    with open(exif_file_path, 'r') as f:
        content = f.read()

    # 查找所需的信息
    awb_tag_date = None
    ae_tag_hw_ver = None
    awb_nvram_unit_gain_r= None
    awb_nvram_unit_gain_g= None
    awb_nvram_unit_gain_b= None
    awb_nvram_golden_gain_r= None
    awb_nvram_golden_gain_g= None
    awb_nvram_golden_gain_b= None
    awb_nvram_unit_gain_r_m= None
    atms_scenario_idx_p2= None

    # for line in content.split('\n'):
    #     if 'AWB_TAG_DATE' in line:
    #         awb_tag_date = line.strip()  # 去除行首尾的空白字符
    #     if 'AE_TAG_HW_VER' in line:
    #         ae_tag_hw_ver = line.strip()  # 去除行首尾的空白字符
    #     if awb_tag_date and ae_tag_hw_ver:
    #         break
    #
    # return image_name, awb_tag_date, ae_tag_hw_ver
    for line in content.split('\n'):
        if 'AWB_TAG_DATE' in line:
            awb_tag_date = line.strip()  # 去除行首尾的空白字符
        if 'AE_TAG_HW_VER' in line:
            ae_tag_hw_ver = line.strip()  # 去除行首尾的空白字符
    return (
        image_name, awb_tag_date, ae_tag_hw_ver
    )
